/**
 * Copyright (c) 2014-2016 www.nryuncang.com. All Rights Reserved.
 */
package com.topisv.tms.service.unprocessxml;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.qimen.DeliveryOrderTask;
import com.topisv.tms.entity.qimen.OrderProcessConfig;
import com.topisv.tms.mybatis.SqlMapDao;
import com.topisv.tms.mybatis.service.CommonService;

/**
 * 
 *
 * @author <a href=mailto:zhangweize@nryuncang.com>zhangweize</a>
 * @version 1.0.0
 */
@Component
@Transactional(readOnly = true)
public class UnProcessXmlService extends CommonService{
	private static final Logger log = LoggerFactory.getLogger(UnProcessXmlService.class);
	
	@Autowired
	private SqlMapDao sqlMapDao;

	
	
	public void sendTask(OrderProcessConfig o, String CustomerCode, String limitNum, String fromDate, String toDate) throws ClientProtocolException, IOException{
		
		List<NameValuePair> parameters = new ArrayList<NameValuePair>();
		parameters.add(new BasicNameValuePair("_warehouseCode", o.getWarehouseCode()));
		parameters.add(new BasicNameValuePair("_customerCode",CustomerCode));
		parameters.add(new BasicNameValuePair("_num",limitNum));
		parameters.add(new BasicNameValuePair("_fromDate",fromDate));
		parameters.add(new BasicNameValuePair("_toDate",toDate));
		
		StringBuffer sb = new StringBuffer();
		HttpClient client = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost("http://"+o.getIp()+":"+o.getPort()+"/wms/v2/unProcessXml/redirectDealIp");
		httpPost.setEntity(new UrlEncodedFormEntity(parameters, Charset.forName("UTF-8")));

		HttpResponse httpResponse = client.execute(httpPost);
		BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), Charset.forName("UTF-8")));
		String content = null;
		while ((content = reader.readLine()) != null) {
//			sb.append(content);
		}
	}
	
	/**
	 * 校验待处理订单书是否大于阀值
	 * 
	 *
	 * @param param
	 * @param o
	 * @return [参数说明]
	 * @author <a href=mailto:zhangweize@nryuncang.com>zhangweize</a>
	 * @version 1.0.0
	 */
	public boolean isMeetWaitOrderNumlessThreshold(Map<String,Object> param, OrderProcessConfig o){
		//根据仓库统计仓库待处理订单数
		Map<String, Map<String, Long>> waitProcessEdiOrderMap = loadEdiOrderMap(param);
		
		//筛选小于仓库订单阀值的仓库
		long orderNum = waitProcessEdiOrderMap.get(o.getWarehouseCode()) == null ? 0l : waitProcessEdiOrderMap.get(o.getWarehouseCode()).get("num");
		
		return o.getThreshold() > orderNum ;
	}
	
	/**
	 * DeliveryOrderTask表统计仓库中未处理的报文数量
	 * 
	 *
	 * @return [参数说明]
	 * @author <a href=mailto:zhangweize@nryuncang.com>zhangweize</a>
	 * @version 1.0.0
	 */
	
	@SuppressWarnings("unchecked")
	public Map<String, Map<String,Long>> loadDeliveryOrderTaskMap (Map<String, Object> param){
		return sqlMapDao.selectMap("deliveryOrderTaskMapper.countXmlNumByWarehouseCode", param, "warehouseCode");
	}
	
	/**
	 * EdiOrder表统计仓库中待处理订单数量和不可处理订单数量
	 * 
	 *
	 * @param param
	 * @return [参数说明]
	 * @author <a href=mailto:zhangweize@nryuncang.com>zhangweize</a>
	 * @version 1.0.0
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Map<String,Long>> loadEdiOrderMap (Map<String, Object> param){
		return sqlMapDao.selectMap("ediOrderMapper.countOrderNumByWarehouseCode", param, "warehouseCode");
	}
	
	
}
